var createError = require('http-errors');
var express = require('express');
var app = require('./upload')
var dao=require('./util/dao')
const cors = require('cors')

const bodyParser = require('body-parser');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// index引入路由，很重要
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var empsRouter=require('./routes/emps')
var goodsRouter=require('./routes/goods')


// var app = express();

// 改写
var http = require('http');
const { addGoods } = require('./controllers/goodsController');




var server = http.createServer(app);

app.use(express.json());

// app.use(express.urlencoded({ extended: false }));

app.use(cookieParser());

// 设置一下静态资源
app.use(express.static('public'))

// app.use(express.static(path.join(__dirname, 'public')));
// post请求
app.use(bodyParser.urlencoded({extended:true}))
app.use('/', indexRouter);
app.use('/v1/users', usersRouter);
app.use('/v1/emps',empsRouter)
app.use('/v1/goods',goodsRouter)



// 错误处理中间件
app.use( (err,req,res,next)=>{
    console.log(err)
    res.status(500).send({err})
} )


// 配置跨域
app.use(
  cors()
);
// var uuid = require('uuid')
// var multer = require('multer')
// var uploadTools = multer({
//     storage: multer.diskStorage({ // 该存储方案将会把文件直接存入磁盘
//         destination: (req, file, callback)=>{
//             callback(null, 'public')
//         },
//         filename: (req, file, callback)=>{
//             // 通过file，获取原始文件名     huangbo.jpg
//             let name = file.originalname       
//             // 截取源文件的后缀 .jpg  .png  ....
//             let ext = name.substr(name.lastIndexOf('.'))
//             // 生成一个随机文件名，调用callback返回即可
//             let newName = uuid.v4() + ext
//             callback(null, newName)
//         }
//     })
// })



// app.post('/upload', uploadTools.single('file'),  (req, resp)=>{
//     // multer中间件将会把文件信息存入：req.files
//     let url =  req.file.filename
//     console.log(req.file)
//     resp.send(dao.ok(url))
// })


// app.all('*', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By", ' 3.2.1')
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
//   });





// 不用暴露出去，监听一下端口
app.listen(3000,()=>{
	console.log('服务器启动成功')
})


